/**
 * <p>Title: ServiceZMCL20.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-14 下午01:35:24  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cl.domain.ZMCLItemAssociation;
import com.baosight.sczx.zm.cl.domain.ZMCLSecurityItems;
import com.baosight.sczx.zm.cl.domain.ZMCLSurveyPoint;

/**
 * <p>Title: ServiceZMCL20.java</p>
 * <p>Description: 安全检查项目管理 </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-14 下午01:35:24  张小飞  Create
 *</pre>
 */
public class ServiceZMCL20 extends ServiceEPBase {
	
	/**
	 * 安全检查项目管理 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCLSecurityItems</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		String itemCode = this.getItemCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdItemCode");
		outInfo.set("itemCode", itemCode);//新增时按照规则生成安全项目编号
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.engineeringType"));//获得检查项目类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.engineeringType", "cmes.cl.engineeringTypeInqu", true));
		
		String itemType = initCombox(InitialData.getInstance().getEiBlockByNo("cmes.cl.engineeringType"), 0);
		outInfo.set("itemType", itemType);//设置下拉框的默认值
		
		return outInfo;
	}
	
	/**
	 * 安全检查项目 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-28              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo)
	{
		EiInfo queryInfo = super.query(inInfo, "ZMCLSecurityItems.query", new ZMCLSecurityItems());
		EiInfo outInfo = getSurveypointName(queryInfo);//获得引用的巡检点名称
		return outInfo;
	}
	
	/**
	 * 获得引用了安全检查项目的巡检点名称
	 * <p>Function: getSurveypointName</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7   高强
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private EiInfo getSurveypointName(EiInfo info)
	{
		EiBlock resultBlock = info.getBlock(EiConstant.resultBlock);
		for(int i=0; i<resultBlock.getRowCount(); i++)
		{
			if(info.getBlock(EiConstant.queryBlock) == null) {
				info.addBlock(EiConstant.queryBlock);
			}
			if(resultBlock.getRow(i).get("fdKeyId") !=null)
			{
				String fdKeyId = resultBlock.getRow(i).get("fdKeyId").toString();
				Map<String, String> querySurveypointIdMap = new HashMap<String, String>();
				querySurveypointIdMap.put("fdItemsId", fdKeyId);//添加查询条件
				List resultSurveypointIdMap = dao.query("ZMCLItemAssociation.query", querySurveypointIdMap);//查询到被引用安全巡检点主键 
				StringBuffer surveypointName = new StringBuffer();
				if(resultSurveypointIdMap.size()!=0)
				{
					for(int j=0; j<resultSurveypointIdMap.size(); j++)
					{
						ZMCLItemAssociation data = (ZMCLItemAssociation)resultSurveypointIdMap.get(j);
						Map<String, String> querySurveypointNameMap = new HashMap<String, String>();
						querySurveypointNameMap.put("fdKeyId", data.getFdSurveypointId());//添加查询条件
						List resultSurveypointNameMap = dao.query("ZMCLSurveyPoint.query", querySurveypointNameMap);//查询到被引用安全巡检点的名称
						for(int k=0; k<resultSurveypointNameMap.size(); k++)
						{
							ZMCLSurveyPoint data1 = (ZMCLSurveyPoint)resultSurveypointNameMap.get(k);
							if(j == resultSurveypointIdMap.size()-1)
							{
								surveypointName.append(data1.getFdSurveypointName());//如果是引用了一个巡检点或者是最后一个巡检点不以分号结束
								break;
							}
							surveypointName.append(data1.getFdSurveypointName()+";");
						}
					}
				}
				resultBlock.getRow(i).put("fdSurveypointName", surveypointName.toString());//取得所有巡检点名称
			}
		}
		return info;
	}
	
	/**
	 * 查询最新的检查编码号 
	 * <p>Function: getItemCode</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-28              高强       
	 * </p>
	 *
	 */
	public EiInfo getItemCode(EiInfo inInfo)
	{
		EiInfo Info = super.query(inInfo, "ZMCLSecurityItems.queryLatestItemCode", new ZMCLSecurityItems());
		EiInfo outInfo = new EiInfo();
		outInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCLSecurityItems().eiMetadata);
		if(Info.getBlock("result").getRowCount() == 0)
		{
			String defaultStr = "AQXM" + "_" + StaticTools.getDateDefault()+ "_" + "001";//如果在当天没有记录生成，则默认为是当天的第一条记录
			outInfo.getBlock("result").setCell(0,"fdItemCode",defaultStr );
		}else
		{
			String str = Info.getBlock(EiConstant.resultBlock).getCell(0, "fdItemCode").toString();
			outInfo.getBlock("result").setCell(0,"fdItemCode", StaticTools.convertStr(str, 14, 17));//编号的前14位AQXM_YYYYMMDD_,全长为17位
		}
		return outInfo;
	}
	
	/**
	 * 安全检查项目 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强         
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo)
	{
		
		boolean validateValue = validate(inInfo, new HashMap<String, String>());//有效性确认
		if(validateValue) {
			return inInfo;
		}
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_SECURITY_ITEMS"));//主键
		inInfo.getBlock("result").setCell(0, "fdStatus", "在用");//新增状态默认为在用
		EiInfo outInfo = super.insert(inInfo, "ZMCLSecurityItems.insert", "result");
		
		String itemCode = this.getItemCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdItemCode");
		outInfo.set("itemCode", itemCode);//新增时按照规则生成安全项目编号
		
		return outInfo;
	}
	
	/**
	 * 安全检查项目 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCLSecurityItems.update");
		return outInfo;
	}
	
	/**
	 * 安全检查项目 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		super.update(inInfo, "ZMCLSecurityItems.delete", "result");
		EiInfo outInfo = this.query(inInfo);
		String itemCode = this.getItemCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdItemCode");
		outInfo.set("itemCode", itemCode);//新增时按照规则生成安全项目编号
		return outInfo;
	}

	
	/**
	 * 检查是否有重复的检查项目编号存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午         高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdItemCode = inInfo.getBlock("result").getCellStr(0, "fdItemCode");
		params.put("fdItemCode", fdItemCode);
		
		List result = dao.query("ZMCLSecurityItems.query", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLSecurityItems _temp = (ZMCLSecurityItems)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdItemCode() != null && _temp.getFdItemCode().equals(fdItemCode)) {
				msg.append(" 该检查项目编号 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 启用某条数据
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午         高强
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLSecurityItems.modifyStart");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 停用某条数据
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午         高强
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLSecurityItems.modifyStop");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: initCombox</p>
	 * <p>Description: block:Info中的block对象； index:block中数据区的行数（从0开始计）</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午05:56:42 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private String initCombox(EiBlock block, int index){
		Map<String,String> map = block.getRow(index);
		String column = "";
		for(String value : map.keySet()){
			if("value".equals(value)){
				column = map.get(value);
				break;
			}
		}
		return column;
	}
}
